package com.joke.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DownloadUtil {
	private static final Logger logger = LoggerFactory.getLogger(DownloadUtil.class);

	public static void main(String[] args) {
//		downloadImg("http:"+"//pic.qiushibaike.com/system/pictures/12012/120122767/medium/app120122767.jpg","D:\\LocoDown\\images\\3018\\123.jpg");
	}
	
	// 下载指定路径图片
	public static boolean downloadImg(String netImg, String filePath) {
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		// 第一步，创建网络连接
		try {
			URL url = new URL(netImg);
			// 第二步，打开连接
			URLConnection urlconnection = url.openConnection();
			// 第三步，获取流对象
			bis = new BufferedInputStream(urlconnection.getInputStream());
			// 第四步，创建本地文件路径
			File file = new File(filePath);
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			// 第五步，下载
			bos = new BufferedOutputStream(new FileOutputStream(file));
			
			byte[] buffer = new byte[1024];
			int len = -1;
			while ((len = bis.read(buffer)) != -1) {
				bos.write(buffer, 0, len);
				bos.flush();
			}

			return true;
		} catch (Exception e) {
		e.printStackTrace();
			return false;
		}finally{
			// 第六步，关闭IO
			try {
				if (bos!=null)bos.close();
			} catch (IOException e) {
			}
			try {
				if (bis!=null)bis.close();
			} catch (IOException e) {
			}
		}
	}
	
	/**
	 * 将入库的文本段子做个本地备份
	 * @param map
	 */
	public static void writeTxT(String path, Object[] txt) {
		
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new FileWriter(path, true));//追加形式
			for (Object str : txt) {
				out.write(str.toString()+"\r\n");
			}
			logger.info("写入本地 "+path+"完毕");
		} catch (IOException e) {
			e.printStackTrace();
		}  finally {
			try {
				out.flush();
			} catch (IOException e) {
				e.printStackTrace();
			} // 把缓存区内容压入文件  
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			} // 最后记得关闭文件  
		}
	}
	
	/**
	 * 将入库的热门,趣图,短视频做个本地备份
	 * @param map
	 */
	public static void writeImage(String path, Map<String, String> hotMap) {
		
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new FileWriter(path, true));//追加形式
			for (String str : hotMap.keySet()) {
				if(!"".equals(str.trim())) {
					out.write(str+"\r\n");// \r\n即为换行  
					out.write(hotMap.get(str)+"\r\n"); 
				}
			}
			logger.info("写入本地 "+path+"完毕");
		} catch (IOException e) {
			e.printStackTrace();
		}  finally {
			try {
				out.flush();
			} catch (IOException e) {
				e.printStackTrace();
			} // 把缓存区内容压入文件  
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			} // 最后记得关闭文件  
		}
	}
	
	// 获取网页源代码
	public static String htmlSource(String link,String encoding){
		StringBuilder stringBuilder = new StringBuilder();
		//1，创建连接
		try {
			URL url=new URL(link);
			//2，打开连接
			URLConnection urlConnection =url.openConnection();
			//3，打开流
			InputStream inputStream = urlConnection.getInputStream();
			//4,利用转换流将字节流转换为字符流，并存入缓冲区
			InputStreamReader isr = new InputStreamReader(inputStream,encoding);
			BufferedReader bufferedReader = new BufferedReader(isr);
			String line = null;
			while((line = bufferedReader.readLine())!=null){
				stringBuilder.append(line+"\r\n");
			}
			//5,关闭
			bufferedReader.close();
			inputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return stringBuilder.toString();
	}
}